#ifndef CADREUTIL_H
#define CADREUTIL_H
#include <QHash>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QComboBox>
#include <QSqlRecord>
#include <QTableView>
#include <QSqlField>
#include <QMessageBox>
#include <QObject>
#include <QDebug>
#include <QSqlError>

class CadreUtil
{
public:
    // 动态生成item项的Combobox
    static QHash<QString,QString> initComboBox(QComboBox *cb, QString valueid,QString table,
                                          QString fieldid,QString fieldvalue)
    {
        QSqlQueryModel *model= new QSqlQueryModel();
        QString sql = QString::fromUtf8("select %1,%2 from %3").arg(fieldid,fieldvalue,table);
        model->setQuery(sql);
        int rows = model->rowCount();
        QHash<QString,QString> hash;
        QStringList stringlist;
        for (int row=0;row<rows;row++) {
           QString k = model->record(row).value(fieldid).toString();
           QString v = model->record(row).value(fieldvalue).toString();
           hash.insert(k,v);
           stringlist << v;
        }
        cb->clear(); //不先清空，多次点击后 会加载多次
        cb->addItems(stringlist);
        cb->addItem("");
        cb->setEditable(true);

        int index = cb->findText(hash[valueid]);
        cb->setCurrentIndex(index);
        return hash;
    }

    // 根据固定值设定下拉框
    // cb:组件名称，valueid:初始值， hash:包含初始值的对应hash表
    static void initYNComboBox(QComboBox *cb,QString valueid,QHash<QString,QString> hash)
    {
        cb->clear();
        foreach(QString key,hash.uniqueKeys()){
            cb->addItem(hash[key]);
        }
        cb->addItem("");
        int index;
        index = cb->findText(hash[valueid]);
        cb->setCurrentIndex(index);
    }


    // 移动一行的公共方法
    // idcolumn  id 所在列
    static void moveRow(QSqlTableModel *model,int idcolumn, int curRow, int toRow){

        if(curRow == toRow)
             return;

        int row;

        if(curRow>toRow){
            row = curRow;
            curRow = toRow;
            toRow = row;
        }
        QSqlRecord curRecord= model->record(curRow);
        QString curId= curRecord.field(idcolumn).value().toString();
        QSqlRecord toRecord= model->record(toRow);
        QString toId = toRecord.field(idcolumn).value().toString();
        model->removeRows(curRow,2); //挨着的两行
        model->insertRow(curRow);
        model->setRecord(curRow,toRecord);
        model->setData(model->index(curRow,idcolumn),curId);
        model->insertRow(toRow);
        model->setRecord(toRow,curRecord);
        model->setData(model->index(toRow,idcolumn),toId);
        int sub = model->submitAll();
    //    qDebug()<< "submit" << sub;
        if(!sub){
            model->revertAll();
            qDebug()<< "error" << model->lastError().databaseText();
            qDebug()<< "error" << model->lastError().driverText();
        }
    }


    // 上移,实现过程：两行数据拿出来，删除两行，插入row,插入record,修改ID。
    // idcolumn  id 所在列
    static int moveUpOne(QSqlTableModel *model,int idcolumn, QTableView *view)
    {
        int curRow = view->currentIndex().row();
        if(curRow==0){
            QMessageBox::information(view,QObject::trUtf8("上移操作提示"),
                QObject::trUtf8("当前第一行，不能进行上移操作"),QMessageBox::Yes);
            return 0;
        }
        // 没有选中任何一行
        if(curRow==-1){
            QMessageBox::information(view,QObject::trUtf8("操作提示"),
                QObject::trUtf8("没有选中任何一行，不能进行操作"),QMessageBox::Yes);
            return 0;
        }
        moveRow(model,idcolumn,curRow,curRow-1);
        return curRow-1;
    }
    // 下移,实现过程：两行数据拿出来，删除两行，插入row,插入record,修改ID。
    // idcolumn  id 所在列
    static int moveDownOne(QSqlTableModel *model,int idcolumn, QTableView *view)
    {
        int curRow = view->currentIndex().row();
        int total = model->rowCount();
        if(curRow==total-1){
            QMessageBox::information(view,QObject::trUtf8("操作提示"),
                QObject::trUtf8("当前最后一行，不能进行下移操作"),QMessageBox::Yes);
            return 0;
        }
        if(curRow==-1){
            QMessageBox::information(view,QObject::trUtf8("操作提示"),
               QObject::trUtf8("没有选中任何一行，不能进行操作"),QMessageBox::Yes);
            return 0;
        }
        moveRow(model,idcolumn,curRow,curRow+1);
        return curRow+1;
    }


    static void deleteRow(QSqlTableModel *model, QTableView *view)
    {
        int index=view->currentIndex().row();
        int ok = QMessageBox::warning(view, QObject::trUtf8("删除当前行!"),
                                        QObject::trUtf8("你确定删除当前行吗？"),
                              QMessageBox::Yes,QMessageBox::No);
         if(ok == QMessageBox::Yes)
         {
             model->removeRow(index);
             view->selectRow(model->rowCount()-1);
         }
    }
    // 获取hash中 value最大的key;
    static int getMinValueFromHash(QHash<int,int> hash)
    {
        int eduri;
        if(hash.size()>1){
            QList<int> indexs;
            for(int i=0;i<hash.values().size();i++){
                indexs << hash.values()[i];
            }
            int min= indexs[0];
            for(int j=0;j<indexs.size();j++){
                if(indexs[j]<min){
                    min = indexs[j];
                }
            }
            eduri= hash.key(min);
        }
        if(hash.size()==1){
            eduri=hash.keys()[0];
        }
        return eduri;
    }
};





#endif // CADREUTIL_H
